package user;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComponent;

/**
 * An object representing who the current user of a system
 * is as well as some pertinent information. 
 * @author Enrique De Diego
 *
 */
public class CurrentUser
{
	private int id;
	private UserPrivilege privLevel;
	private String username;
	private RestrictionManager restrictions;
	
	public CurrentUser()
	{
		id = 0;
		privLevel = UserPrivilege.FAN;
		username = "defaultUser";

	}
	
	public CurrentUser(int id, UserPrivilege privLevel, String username)
	{
		this.id = id;
		this.privLevel = privLevel;
		this.username = username;
		this.restrictions = new RestrictionManager(this);

	}
	
	public CurrentUser(int id, int privLevel, String username)
	{
		this.id = id;
		this.privLevel = UserPrivilege.convertIntToPriv(privLevel);
		this.username = username;
		this.restrictions = restrictions;
	}
	
	public static void main(String[] args)
	{
		CurrentUser user = new CurrentUser(0,UserPrivilege.FAN, "enrique");
		
		/*
		 * Demonstrating using isRestricted() for conditionals
		 */
		if (user.isRestricted(UserPrivilege.ADMIN))
			System.out.println("YOU SHALL NOT PASS!");
		else
			System.out.println("Move along.");
		
		/*
		 * Demonstrating how to restrict user actions
		 * using the UserAction event listener
		 */
		JButton button = new JButton();
		button.addActionListener( new UserAction(user,UserPrivilege.ADMIN) 
		{

			@Override
			public void onRestricted(ActionEvent e) 
			{
				System.out.println("YOU SHALL NOT PASS!");
				
			}

			@Override
			public void onNotRestricted(ActionEvent e) 
			{
				System.out.println("Move along.");
			}
			
		});
		button.doClick();
		
		/*
		 * Demonstrating how to register a component as restricted 
		 */
		user.addRestriction(button, UserPrivilege.ADMIN);
		button.doClick(); //should do NOTHING!
		
		/*
		 * Above test should print:
		 * YOU SHALL NOT PASS!
		 * YOU SHALL NOT PASS!
		 * -- Success!
		 */
		
	}
	
	public int getId()
	{
		return id;
	}

	public int getPrivLevel()
	{
		return privLevel.getLevel();
	}

	public String getUsername()
	{
		return username;
	}
	
	/**
	 * Check  whether user can do a certain action
	 * @param checkLevel - the UserPrivilege necessary to do the action
	 * @return true if the other is restricted from doing that, false otherwise
	 */
	public boolean isRestricted(UserPrivilege priv)
	{
		int checkLevel = priv.getLevel();
		if (getPrivLevel() < checkLevel)
			return false;
		return true;
	}
	
	/**
	 * Check  whether user can do a certain action
	 * @param checkLevel - the level of privilege necessary to do the action
	 * @return true if the other is restricted from doing that, false otherwise
	 */
	public boolean isRestricted(int checkLevel)
	{
		if (getPrivLevel() < checkLevel)
			return false;
		return true;
	}
	
	public void addRestriction(JComponent comp, UserPrivilege priv)
	{
		restrictions.registerRestriction(comp, priv);
	}
	
	public String toString()
	{
		return "Username: " + username + ", id: " + id + ", level: " + privLevel.getLevel();
	}
	
}


